<?php

class MainController extends Controller {

    public function actions() {
        return array(
            'chat' => array(
                'class' => 'ext.realtimeChatWidget.RealtimeChatAction',
            ),
        );
    }

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations			
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'server', 'chat', 'error', 'permission'),
                'users' => array('@'),
            ),
            array('allow',
                'actions' => array('modelCheck'),
                'users' => array('*'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionModelCheck($key) {
        if ($key != '258555')
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        $urls = array(
            'http://market.yandex.ru/guru.xml?CMD=-RR%3D9%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D160043-EXC%3D1-PG%3D10&hid=91491' => 1,
//            'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D6427101-EXC%3D1-PG%3D10&hid=6427100' => 2,
//            'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D432460-EXC%3D1-PG%3D10&hid=91013' => 3,
//            'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D108206-EXC%3D1-PG%3D10&hid=90639' => 9,
//            'http://market.yandex.ru/guru.xml?CMD=-RR%3D9%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D100514-EXC%3D1-PG%3D10&hid=91148' => 6
        );
        Yii::import('ext.phpQuery');
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 100;
        foreach ($urls as $url => $categoryID) {
            $str = '';
            $output = mTools::getYaContent($url, array('ip' => '201.70.183.134', 'port' => '3128'), $limit);
            echo '<pre>';
            print_r($output);
            exit;
            $document = phpQuery::newDocument($output[0]);
            $pager = pq($document['.b-pager__pages'])->html();
            if (!empty($pager)) {
                $nextPage = '';
                $flag = false;
                $nextPage = pq($document['.b-pager span .b-pager__next'])->attr('href');
                while (!$flag) {
                    sleep(1);
                    $out = mTools::getYaContent('http://market.yandex.ru/' . $nextPage, array('ip' => '190.92.87.102', 'port' => '8080'), $limit);
                    $d = phpQuery::newDocument($out[0]);
                    $nextPage = pq($d['.b-pager span .b-pager__next'])->attr('href');
                    if (empty($nextPage))
                        $flag = true;
                    $str .= $out[0];
                }
            }
            $doc = phpQuery::newDocument($output[0] . $str);
            echo pq($doc['.b-offers__desc .b-offers__title .b-offers__name'])->text() . '<br />';
        }
    }

    public function actionPermission() {
        $this->pageTitle = 'У вас недостаточно прав доступа для этой операции';

        $this->render('permission');
    }

    public function actionIndex() {
        $this->render('index', array('status' => mTools::getServerStatus(TRUE)));
    }

    public function actionError() {
        $this->pageTitle = 'К сожалению, страница не найдена';
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionServer() {
        if (!Yii::app()->request->isAjaxRequest)
            return false;

        $result = mTools::getServerStatus();

        echo json_encode($result);
    }

}
